package client

import (
	"context"
	"fmt"
	"github.com/go-kit/kit/auth/jwt"
	transport_grpc "github.com/go-kit/kit/transport/grpc"
	"google.golang.org/grpc"
	"gysa-share01/exception"
	user_service "gysa-share01/user-service"
	"gysa-share01/user-service/user"
	service_user "gysa-share01/user-service/user/proto"
)

func Info(request *service_user.InfoRequest, ctx context.Context) (*service_user.InfoResponse, error) {
	serviceAddress := fmt.Sprintf("%s:%s", user_service.GrpcHost, user_service.GrpcPort)
	conn, err := grpc.Dial(serviceAddress, grpc.WithInsecure())
	rep := &service_user.InfoResponse{}
	if err != nil {
		return rep, err
	}
	defer conn.Close()

	///service.user.User/Info
	endpoint := transport_grpc.NewClient(
		conn,
		"service.user.User",
		"Info",
		user.DecodeRequest,
		user.EncodeResponse,
		&service_user.InfoResponse{},
		transport_grpc.ClientBefore(jwt.ContextToGRPC()),
	).Endpoint()

	userInfo, err := endpoint(ctx, request)
	if err != nil {
		return rep, err
	}
	rep, ok := userInfo.(*service_user.InfoResponse)
	if !ok {
		rep.ErrCode = int32(exception.SYSTEM_ERROR)
		rep.ErrMsg = "返回格式错误"
		return rep, nil
	}

	return rep, nil
}
